<template>
  <div class="container">
    <el-card>
      <el-form>
        <div class="filter-container">
          <div>
            <el-form-item>
              学科名称
              <el-input
                style="width: 200px"
                class="filter-item"
                v-model="subjects.subjectName"
              ></el-input>
              <el-button class="filter-item" size="small" @click="eliminate"
                >清除</el-button
              >
              <el-button
                class="filter-item"
                size="small"
                type="primary"
                @click="getsubject"
                >搜索</el-button
              >
            </el-form-item>
          </div>
          <div>
            <el-button
              type="success"
              size="small"
              @click="dialogFormVisible = true"
              ><span class="el-icon-edit">新增学科</span></el-button
            >
          </div>
        </div>
      </el-form>
      <!-- 提示 -->
      <el-alert :title="alertText" type="info" class="alert" show-icon>
      </el-alert>
      <!-- /提示 -->

      <!-- 表格 -->
      <el-table style="width: 100%" :data="subjectList">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创建者" width="100">
        </el-table-column>
        <el-table-column label="创建日期" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template slot-scope="scope">
            <span>{{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-link type="primary" @click="kexuefeilei(scope.row)">科学分类</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="xuekebiaoqian(scope.row)">科学标签</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="amendFrom(scope.row)">修改</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="removeSubjects(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格 -->

      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(subjects.page)"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="Number(subjects.pagesize)"
          background
          layout="prev, pager, next, sizes,jumper"
          :total="Number(total)"
        >
        </el-pagination>
      </div>
      <!-- 分页 -->

<!-- 新增弹出对话框 -->
      <el-dialog title="新增学科" :visible.sync="dialogFormVisible" @close="addDialogClose" width="400px">
        <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
          <el-form-item label="学科名称" prop="username">
            <el-input v-model="addForm.username"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary"
            @click="addSubjects" >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- /新增弹出对话框 -->

      <!-- 修改弹出对话框 -->
      <el-dialog title="修改学科" :visible.sync="modificationVisible" @close="updateDialogClose" width="400px">
        <el-form :model="updateForm" ref="updateFormRef" label-width="100px">
          <el-form-item label="学科名称">
            <el-input v-model="updateList.subjectName"></el-input>
          </el-form-item>
          <el-form-item label="是否显示">
            <el-switch
              v-model="value"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="modificationVisible = false">取 消</el-button>
          <el-button type="primary"
           @click="updateSubjects" >确 定</el-button
          >
        </div>
      </el-dialog>
      <!-- /修改弹出对话框 -->
    </el-card>
  </div>
</template>

<script>
import { list as subject, add, update, remove } from '@/api/hmmm/subjects'

// import subjectsAdd from '../components/subjects-add'

export default {
  name: '',
  components: {},
  props: {},
  data () {
    return {
      subjects: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectName: '' // 学科名称
      },
      subjectList: [],
      total: null,
      alertText: '',
      dialogFormVisible: false, // 控制新增对话框的显示和隐藏
      value: true,
      // 添加用户表单数据
      addForm: {
        username: ''
      },
      // 表单验证规则对象
      addFormRules: {
        username: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },
      addFormList: {
        subjectName: '', // 学科名称
        isFrontDisplay: 1 // 是否在页面显示 1是 0否
      },
      modificationVisible: false,
      updateForm: {},
      updateList: {
        id: null,
        subjectName: '',
        isFrontDisplay: true
      },
      removeList: {
        id: null
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getsubject()
  },
  mounted () {},
  methods: {
    async getsubject () {
      // 读取数据
      const { data } = await subject(this.subjects)
      // console.log(data)
      this.subjectList = data.items
      // console.log(this.subjectList)
      this.total = data.counts
      this.alertText = `数据一共${data.counts}条`
    },
    // 监听 pagesize 改变的事件
    handleSizeChange (newSize) {
      this.subjects.pagesize = newSize
      if (this.subjects.page === 1) {
        this.getsubject(this.subjects)
      }
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.subjects.page = newPage
      this.getsubject()
    },
    eliminate () {
      this.subjects.subjectName = ''
      this.getsubject()
    },
    // 监听添加用户对话框的关闭事件
    addDialogClose () {
      this.$refs.addFormRef.resetFields()
    },
    addSubjects () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加用户的网络请求
        this.addFormList.subjectName = this.addForm.username
        await add(this.addFormList)
        /* if (res.meta.status !== 200) {
          this.$message.error('添加用户失败')
        } */
        /* if (this.addFormList.subjectName === this.subjectList.subjectName) {
          return this.$message.error('学科已存在，请重新输入')
        } */
        // console.log(res)
        this.$message.success('添加用户成功')
        // 关闭弹窗
        this.dialogFormVisible = false
        // 重新获取用户数据
        this.getsubject()
      })
    },
    updateDialogClose () {
      this.$refs.updateFormRef.resetFields()
    },
    async amendFrom (scope) {
      this.modificationVisible = true
      this.updateList.subjectName = scope.subjectName
      this.updateList.id = scope.id
      // console.log(scope)
      // const { data } = await update({ id: scope.id, subjectName: scope.subjectName })
      // console.log(data)
    },
    async updateSubjects () {
      // console.log(this.updateList)
      await update(this.updateList)
      // console.log(data)
      if (this.updateList.subjectName !== '') {
        // 关闭弹窗
        this.modificationVisible = false
        this.$message.success('修改成功')
        // 重新渲染页面
        this.getsubject()
      } else {
        return this.$message.info('学科不能为空')
      }
    },
    // 根据id删除用户信息
    async removeSubjects (id) {
      // console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      this.removeList.id = id
      await remove(this.removeList)
      this.$message.success('删除成功')
      this.getsubject()
    },
    kexuefeilei (scope) {
      this.$router.push({ path: '/subjects/directorys', query: { id: scope.id, name: scope.subjectName } })
    },
    xuekebiaoqian (scope) {
      this.$router.push({ path: '/subjects/tags', query: { id: scope.id, name: scope.subjectName } })
    }

  }
}
</script>

<style scoped lang='less'>
.container {
  padding: 20px;
}
.filter-container {
  display: flex;
  justify-content: space-between;
  .el-input {
    margin-left: 10px;
    margin-right: 10px;
  }
}
.el-alert {
  margin-bottom: 20px;
}
.el-pagination {
  float: right;
  margin-top: 20px;
  margin-bottom: 20px;
}
</style>
